package com.findtech.threePomelos.login;

import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.SaveCallback;
import com.avos.avoscloud.SignUpCallback;
import com.findtech.threePomelos.base.MyActionBarActivity;
import com.findtech.threePomelos.base.MyApplication;
import com.findtech.threePomelos.R;
import com.findtech.threePomelos.net.NetWorkRequest;
import com.findtech.threePomelos.utils.RequestUtils;
import com.findtech.threePomelos.activity.HomeActivity;
import com.findtech.threePomelos.utils.ToastUtil;
import com.findtech.threePomelos.view.datepicker.DatepickerDialog;


public class SetBabyBirthdayActivity extends MyActionBarActivity implements View.OnClickListener {


    private Button btnNext = null;
    private Button btnCancel = null;
    private Button btnComfirm = null;
    private TextView txtBirthdate = null;
    private LinearLayout layoutDatepicker = null;
    String mBirthday;
    private DatepickerDialog mChangeBirthDialog;
    private ProgressDialog progressDialog;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MyApplication.getInstance().addActivity(this);
        setContentView(R.layout.set_baby_birthday);
        setToolbar(getResources().getString(R.string.title_baby_birthday_info), true);
        init();
        initProgressDialog();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    void init() {
        txtBirthdate = (TextView) findViewById(R.id.txt_birthdate);
        btnNext = (Button) findViewById(R.id.btn_birthdate_next);
        btnCancel = (Button) findViewById(R.id.btn_datepicker_cancel);
        btnComfirm = (Button) findViewById(R.id.btn_datepicker_confirm);
        layoutDatepicker = (LinearLayout) findViewById(R.id.datapicker_birthday_layout);
        txtBirthdate.setOnClickListener(this);
        btnNext.setOnClickListener(this);
        btnCancel.setOnClickListener(this);
        btnComfirm.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_birthdate_next:
                Log.d("ZZ", "onClick registerUserAccount");
                registerUserAccount();
                break;
            case R.id.txt_birthdate:
                if (mChangeBirthDialog != null && mChangeBirthDialog.isShowing()) {
                    return;
                }
                mChangeBirthDialog = new DatepickerDialog(
                        SetBabyBirthdayActivity.this);
                setWindowSize(mChangeBirthDialog);
                mChangeBirthDialog.show();
                mChangeBirthDialog.setBirthdayListener(new DatepickerDialog.OnBirthListener() {
                    @Override
                    public void onClick(String year, String month, String day) {
                        txtBirthdate.setText(year + "-" + month + "-" + day);
                    }
                });
                break;
            case R.id.btn_datepicker_cancel:
                layoutDatepicker.setVisibility(View.GONE);
        }
    }

    void registerUserAccount() {
        progressDialog.show();
        AVUser user = new AVUser();
        user.setUsername(RequestUtils.getSharepreference(this).getString(RequestUtils.USERNAME, null));
        user.setPassword(MyApplication.getPassword());
        user.setMobilePhoneNumber(RequestUtils.getSharepreference(this).getString(RequestUtils.USERNAME, null));

        Log.d("ZZ", "RequestTools.getSharepreference(this).getString(RequestTools.USERNAME, null) = " + RequestUtils.getSharepreference(this).getString(RequestUtils.USERNAME, null));
        Log.d("ZZ", "MyApplication.getPassword() = " + MyApplication.getPassword());

        if (TextUtils.isEmpty(txtBirthdate.getText().toString())) {
            progressDialog.dismiss();
            ToastUtil.showToast(SetBabyBirthdayActivity.this, "宝贝生日不能为空！");
            return;
        }

        if (!TextUtils.isEmpty(MyApplication.getPassword()) &&
                !TextUtils.isEmpty(RequestUtils.getSharepreference(this).getString(RequestUtils.USERNAME, null))) {
            Log.d("ZZ", "signUpInBackground");
            user.signUpInBackground(new SignUpCallback() {
                @Override
                public void done(AVException e) {
                    if (e == null) {
                        // successfully
                        Log.d("ZZ", "SetBabyBirthdayActivity registerUserAccount successfully ");
                        setBabyInfo();
                    } else {
                        progressDialog.dismiss();
                        if (e.getCode() == 214) {
                            ToastUtil.showToast(SetBabyBirthdayActivity.this, "该手机号已经被注册！");
                        }
                        Log.d("ZZ", "SetBabyBirthdayActivity registerUserAccount e = " + e);
                        // failed
                    }
                }
            });
        } else {
            progressDialog.dismiss();
            ToastUtil.showToast(SetBabyBirthdayActivity.this, "用户名或密码不能为空！");
        }
    }

    void setBabyInfo() {
        AVObject post = new AVObject(NetWorkRequest.BABY_INFO);
        post.put(RequestUtils.BABYSEX, RequestUtils.getSharepreference(this).getString(RequestUtils.BABYSEX, ""));
        post.put(RequestUtils.BIRTHDAY, txtBirthdate.getText().toString());
        //缺头像
        post.put("post", AVUser.getCurrentUser());
        post.saveInBackground(new SaveCallback() {
            @Override
            public void done(AVException e) {
                progressDialog.dismiss();
                if (e == null) {
                    ToastUtil.showToast(getApplicationContext(), "注册用户成功！");
                    Intent intent = new Intent(SetBabyBirthdayActivity.this, HomeActivity.class);
                    startActivity(intent);
                } else {
                    Log.d("ZZ", "SetBabyBirthdayActivity AVException e = " + e);
                    ToastUtil.showToast(getApplicationContext(), "保存信息失败！");
                    e.printStackTrace();
                }
            }
        });
    }

    private void setWindowSize(Dialog dialog) {
        DisplayMetrics dm = new DisplayMetrics();
        Window dialogWindow = dialog.getWindow();
        WindowManager m = dialogWindow.getWindowManager();
        m.getDefaultDisplay().getMetrics(dm);
        // 为获取屏幕宽、高
        WindowManager.LayoutParams p = dialogWindow.getAttributes(); // 获取对话框当前的参数值
        dialogWindow.getDecorView().setPadding(0, 0, 0, 0);
        p.width = dm.widthPixels;
        Log.d("cdgcdg", " dm.widthPixels :" + dm.widthPixels);
        p.alpha = 1.0f; // 设置本身透明度
        p.dimAmount = 0.6f; // 设置黑暗度
        p.gravity = Gravity.BOTTOM;
        dialogWindow.setAttributes(p);
    }

    private void initProgressDialog() {
        progressDialog = new ProgressDialog(this);
        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progressDialog.setMessage("注册用户，请稍后...");
        progressDialog.setIndeterminate(false);
        progressDialog.setCancelable(true);
        progressDialog.setCanceledOnTouchOutside(false);
    }
}
